package com.frank.oj.service.admin.problem.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frank.model.dto.CompileDTO;
import com.frank.model.entity.problem.Problem;
import com.frank.model.entity.problem.ProblemCase;
import com.frank.oj.common.exception.StatusFailException;
import com.frank.oj.common.exception.StatusForbiddenException;
import com.frank.oj.common.result.CommonResult;
import com.frank.oj.common.result.ResultStatus;
import com.frank.oj.manager.AdminProblemManager;
import com.frank.oj.model.dto.ProblemDTO;
import com.frank.oj.service.admin.problem.AdminProblemService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author frank
 * @Date 2024/4/2
 */
@Service
public class AdminProblemServiceImpl implements AdminProblemService {
    @Resource
    private AdminProblemManager adminProblemManager;
    @Override
    public IPage<Problem> getProblemList(Integer limit, Integer currentPage, String keyword, Integer auth, String oj) {
        return adminProblemManager.getProblemList(limit,currentPage,keyword,auth,oj);
    }

    @Override
    public CommonResult<Problem> getProblemById(Long pid) {
        try {
            Problem problem = adminProblemManager.getProblemById(pid);
            return CommonResult.successResponse(problem);
        } catch (StatusForbiddenException e) {
            return CommonResult.errorResponse(e.getMessage(), ResultStatus.FORBIDDEN);
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    @Override
    public CommonResult<List<ProblemCase>> getProblemCases(Long pid, Boolean isUpload) {
        List<ProblemCase> problemCaseList = adminProblemManager.getProblemCases(pid, isUpload);
        return CommonResult.successResponse(problemCaseList);
    }

    @Override
    public CommonResult<Void> addProblem(ProblemDTO problemDTO) {

        try {
            adminProblemManager.addProblem(problemDTO);
            return CommonResult.successResponse();
        } catch (Exception e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    @Override
    public CommonResult<Void> importRemoteOJProblem(String name, String problemId) {
        try {
            adminProblemManager.importRemoteOJProblem(name, problemId);
            return CommonResult.successResponse("导入新题目成功");
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    @Override
    public CommonResult<Void> updateProblem(ProblemDTO problemDTO) {
        try {
            adminProblemManager.updateProblem(problemDTO);
            return CommonResult.successResponse();
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        } catch (StatusForbiddenException e) {
            return CommonResult.errorResponse(e.getMessage(), ResultStatus.FORBIDDEN);
        }
    }

    @Override
    public CommonResult<Void> deleteProblem(Long pid) {
        try {
            adminProblemManager.deleteProblem(pid);
            return CommonResult.successResponse();
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    @Override
    public CommonResult<Void> changeProblemAuth(Problem problem) {
        try {
            adminProblemManager.changeProblemAuth(problem);
            return CommonResult.successResponse();
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        } catch (StatusForbiddenException e) {
            return CommonResult.errorResponse(e.getMessage(), ResultStatus.FORBIDDEN);
        }
    }

    @Override
    public CommonResult compileSpj(CompileDTO compileDTO) {
        return adminProblemManager.compileSpj(compileDTO);
    }

    @Override
    public CommonResult compileInteractive(CompileDTO compileDTO) {
        return adminProblemManager.compileInteractive(compileDTO);
    }

    @Override
    public CommonResult<Void> deleteProblems(List<Long> pids) {
        if (pids.isEmpty()){
            return CommonResult.errorResponse("请选择要删除的题目");
        }
        for (Long pid : pids) {
            try {
                adminProblemManager.deleteProblem(pid);
            } catch (StatusFailException e) {
                return CommonResult.errorResponse(e.getMessage());
            }
        }
        return CommonResult.successResponse();
    }
}
